/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listcontrol;

import controllers.DatabaseController;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 *
 * @author gravena
 */
public class CounterThread implements Runnable {

    public static final long TIMEOUT = 1000; /* Time between list version annoucements */

    private DatabaseController dbController;
    private ArrayList<InetAddress> interfaces;

    public CounterThread(DatabaseController dbController) {
        //SE POSSÍVEL, USAR APENAS OS MÉTODOS DO DBCONTROLLER, SEM ACESSAR LOCAL LIST
        this.dbController = dbController;
        this.interfaces = new ArrayList<>();
        try {
            //this.interfaces.add(InetAddress.getByName("172.16.255.255"));
            //this.interfaces.add(InetAddress.getByName("172.27.255.255"));
            this.interfaces.add(InetAddress.getByName("192.168.56.255"));
        } catch (UnknownHostException ex) {
            System.err.println(ex.getMessage());
        }
    }

    @Override
    public void run() {

        while (true) {

            try {
                // Send packet here
                byte[] packetBuffer;
                String UUIDString = dbController.getLocalUUIDString();
                Long localVersion;
                synchronized(this.dbController) {
                    localVersion = dbController.getLocalVersion();
                }
                packetBuffer = this.makePacket(UUIDString, localVersion);
                DatagramPacket packet = new DatagramPacket(packetBuffer, packetBuffer.length);
                packet.setPort(ControlThread.PORT);

                for (int i = 0; i < this.interfaces.size(); i++) {
                    packet.setAddress(this.interfaces.get(i)); // Tem que arrumar um jeito melhor de fazer isso                
                    DatagramSocket sock = new DatagramSocket();
                    sock.setBroadcast(true);
                    sock.send(packet);
                }


                Thread.sleep(TIMEOUT);
            } catch (SocketException ex) {
                System.err.println(ex.getMessage());
            } catch (IOException ex) {
                System.err.println(ex.getMessage());
            } catch (InterruptedException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }

    public byte[] makePacket(String uuid, long version) throws IOException {
        byte[] sBuffer = uuid.getBytes();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.write(sBuffer);
        dos.writeLong(version);

        dos.flush();
        byte[] data = bos.toByteArray();

        return data;
    }

    public void sendPacket(InetAddress ip, byte[] packet) {
    }
}
